package com.leetcode.程序员面试金典.hard;

import java.util.Arrays;

/**
 * @author 覃国强
 * @date 2022-02-15 18:48
 */
public class 面试题_17_19_消失的两个数字 {

  public static void main(String[] args) {
    Solution solution = new Solution();
    int[] nums = {1};
    int[] result = solution.missingTwo(nums);
    System.out.println(Arrays.toString(result));
  }

  static
  class Solution {
    public int[] missingTwo(int[] nums) {
      int n = nums.length + 2;
      int sumN = 0;
      int squareN = 0;
      for (int i = 1; i <= n; ++i) {
        sumN += i;
        squareN += i * i;
      }

      int sumNums = 0;
      int squareNums = 0;
      for (int num : nums) {
        sumNums += num;
        squareNums += num * num;
      }

      int x = squareN - squareNums;
      int y = sumN - sumNums;

      int a = ((2 * y) + (int)(Math.sqrt(4 * y * y - 8 * (y * y - x)))) / 4;

      int b = y - a;

      return new int[] {a, b};
    }
  }

}
